<?php
header("content-type:text/html;charset=utf8");
include_once ("/Classes/PHPExcel.php");
$pdo = new PDO("mysql:host=127.0.0.1;dbname=mysql;charset=utf8",'root','root');
$sql = "select * from mhf";
$res = $pdo->query($sql);
$data = $res->fetchAll(PDO::FETCH_ASSOC);
//print_r($data);die;
$excel = new PHPExcel();
$sheet = $excel->setActiveSheetIndex(0);
$sheet->setCellValue('A1','ID')
      ->setCellValue("B1","name")
      ->setCellValue("C1","age")
      ->setCellValue("D1","money");
$i = 2;
foreach ($data as $k => $v)
{
    $sheet->setCellValue('A'.$i,$v['id'])
        ->setCellValue("B".$i,$v['name'])
        ->setCellValue("C".$i,$v['age'])
        ->setCellValue("D".$i,$v['money']);
$i++;
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');


$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$objWriter->save('php://output');
exit;

?>